package design_pattern.observer.jdkObserver;


import lombok.Data;

import java.util.Observable;
import java.util.Observer;

/**
 * Observer对象是观察者，Observable对象是被观察者。
 */
@Data
public class Sheep implements Observer {

    private SheepState state = SheepState.SLEEPING;
    private String name;

    public Sheep(String name) {
        this.name = name;
    }

    /**
     * 会在接到通知后，拿到Observable对象（被观察者）
     */
    public void update(Observable o, Object arg) {
        // TODO
        Wolf wolf = (Wolf) o;
        WolfState wolfState = (WolfState) arg;
        System.out.println(this.getName() + " discover " + wolf.getName() + " crying " + wolfState);

        if (wolfState == WolfState.HUNGRY) {
            setState(SheepState.RUNNING);
        } else {
            setState(SheepState.SLEEPING);
        }
    }

}
